import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置NProgress
NProgress.configure({ showSpinner: false })

const routes: RouteRecordRaw[] = [
  {
    path: '/test',
    name: 'Test',
    component: () => import('@/views/test.vue'),
    meta: {
      title: '测试页面',
      requiresAuth: false
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      requiresAuth: false
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/layout/index.vue'),
    redirect: '/dashboard',
    meta: {
      title: '首页',
      requiresAuth: true
    },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '仪表盘',
          icon: 'Odometer',
          requiresAuth: true
        }
      },
      {
        path: 'product',
        name: 'Product',
        meta: {
          title: '商品管理',
          icon: 'Goods',
          requiresAuth: true
        },
        children: [
          {
            path: 'list',
            name: 'ProductList',
            component: () => import('@/views/product/list.vue'),
            meta: {
              title: '商品列表',
              requiresAuth: true
            }
          },
          {
            path: 'add',
            name: 'ProductAdd',
            component: () => import('@/views/product/add.vue'),
            meta: {
              title: '添加商品',
              requiresAuth: true,
              hideInMenu: true
            }
          },
          // {
          //   path: 'category',
          //   name: 'ProductCategory',
          //   component: () => import('@/views/product/category.vue'),
          //   meta: {
          //     title: '商品分类',
          //     requiresAuth: true
          //   }
          // }
        ]
      },
      {
        path: 'order',
        name: 'Order',
        component: () => import('@/views/order/list.vue'),
        meta: {
          title: '订单管理',
          icon: 'Document',
          requiresAuth: true
        }
      },
      {
        path: 'inventory',
        name: 'Inventory',
        component: () => import('@/views/inventory/list.vue'),
        meta: {
          title: '库存管理',
          icon: 'Box',
          requiresAuth: true
        }
      },
      {
        path: 'member',
        name: 'Member',
        component: () => import('@/views/member/list.vue'),
        meta: {
          title: '会员管理',
          icon: 'User',
          requiresAuth: true
        }
      },
      {
        path: 'user',
        name: 'User',
        meta: {
          title: '用户管理',
          icon: 'UserFilled',
          requiresAuth: true
        },
        children: [
          {
            path: 'list',
            name: 'UserList',
            component: () => import('@/views/user/list.vue'),
            meta: {
              title: '用户列表',
              requiresAuth: true
            }
          },
          {
            path: 'role',
            name: 'UserRole',
            component: () => import('@/views/user/role.vue'),
            meta: {
              title: '角色管理',
              requiresAuth: true
            }
          }
        ]
      }
    ]
  },
  // {
  //   path: '/:pathMatch(.*)*',
  //   name: 'NotFound',
  //   component: () => import('@/views/error/404.vue'),
  //   meta: {
  //     title: '页面不存在',
  //     requiresAuth: false
  //   }
  // }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  NProgress.start()

  const userStore = useUserStore()

  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    if (!userStore.token) {
      next('/login')
      return
    }
  }

  // 如果已登录且访问登录页，重定向到首页
  if (to.path === '/login' && userStore.token) {
    next('/')
    return
  }

  next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router
